﻿using System.Collections.Generic;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class Feat11XMap
    {
        private List<Feat11XMapEntry> rgXmap = new List<Feat11XMapEntry>();

        public Feat11XMap(BinaryReader reader)
        {
            var iXmapMac = reader.ReadUInt16();
            for (int i  = 0; i < iXmapMac; i++)
            {
                Feat11XMapEntry item = new Feat11XMapEntry();
                item.Read(reader);
                rgXmap.Add(item);
            }
        }

        public void Write(BinaryWriter writer)
        {
            ushort u = (ushort) rgXmap.Count;
            writer.Write(u);
            foreach (var item in rgXmap)
                item.Write(writer);
        }
    }

    internal class Feat11XMapEntry
    {
        private bool fLoadXMap;
        private bool fCanBeSingle;
        private Feat11XMapEntry2 details;

        public void Read(BinaryReader reader)
        {
            uint abcd = reader.ReadUInt32();
            this.fLoadXMap = (abcd & 0x2) != 0;
            this.fCanBeSingle = (abcd & 0x04) != 0;
            details = new Feat11XMapEntry2();
            details.Read(reader);
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(this.Abcd);
            this.details.Write(writer);
        }

        protected uint Abcd 
        {
            get {
                uint result = 0;
                if (fLoadXMap)
                    result |= 0x02;
                if (fCanBeSingle)
                    result |= 0x04;
                return result;
            }
        }
    }
}